package com.ddpt.platform.common.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

@Data
public class CommonPage extends BaseEntity {

    private Integer showCount = 10; // 每页显示记录数

    private Integer totalPage = 0; // 总页数

    private Integer totalResult = 0; // 总记录数

    private Integer currentPage = 1; // 当前页

    /**
     * 转换成mybatisPlus需要的page对象
     */
    public Page<?> parseToMp(){
        return new Page<>(currentPage, showCount);
    }

    /**
     * 将mybatisPlus的分页转换成系统分页对象
     */
    public static CommonPage resolveMp(Page<?> page){
        CommonPage commonPage = new CommonPage();
        if(page == null){
            return commonPage;
        }
        commonPage.setCurrentPage(Long.valueOf(page.getCurrent()).intValue());
        commonPage.setShowCount(Long.valueOf(page.getSize()).intValue());
        commonPage.setTotalResult(Long.valueOf(page.getTotal()).intValue());
        commonPage.setTotalPage(Long.valueOf(page.getPages()).intValue());
        return commonPage;
    }

    public static CommonPage resolveCustomPageResponse(PageResponse<?> page) {
        CommonPage commonPage = new CommonPage();
        if(page == null){
            return commonPage;
        }
        commonPage.setCurrentPage(page.getCurrent().intValue());
        commonPage.setShowCount(page.getSize().intValue());
        commonPage.setTotalResult(page.getTotal().intValue());
        commonPage.setTotalPage(page.getPages().intValue());
        return commonPage;
    }
}
